package org.tang.sharding.jdbc.service;

import org.tang.sharding.jdbc.entity.OrderInfo;
import org.tang.sharding.jdbc.repository.OrderInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 订单信息
 */
@Service
public class OrderInfoService {

    @Autowired
    private OrderInfoRepository orderInfoRepository;

    public void save(Integer count) {
        for (int j = 0; j < count; j++) {

            long userId = j;
            for (int i = 0; i < count; i++) {
                OrderInfo orderInfo = new OrderInfo();
                orderInfo.setOrderName("霹雳防寒金缕衣_"+j+"_"+i);
                orderInfo.setOrderStatus(1);

                orderInfo.setUserId(userId);
                orderInfoRepository.save(orderInfo);
            }
        }

    }

    /**
     * 查询全部订单信息
     *
     * @return 全部订单信息
     */
    public List<OrderInfo> getAll() {
        return orderInfoRepository.findAll();
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    public Optional findById(Long id)
    {
        return orderInfoRepository.findById(id);
    }
}
